import Vue from "vue";
import VueRouter from "vue-router";
import Login from "@/components/Login.vue";
import Home from "@/components/Home.vue";

//  权限
import Rights from "@/views/rights/rights";
import Roles from "@/views/rights/roles";

//  用户
import Users from "@/views/users/users";

//  商品
import Goods from "@/views/goods/goods";
import Params from "@/views/goods/params";
import Categories from "@/views/goods/categories";
import GoodsAdd from "@/views/goods/goodsAdd";

//   订单
import Order from "@/views/order/order";
import Reports from "@/views/reports/reports";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/Login"
  },
  {
    path: "/Login",
    name: "Login",
    component: Login
  },
  {
    path: "/Home",
    name: "Home",
    component: Home,
    children: [
      { path: "/rights", component: Rights },
      { path: "/roles", component: Roles },
      { path: "/users", component: Users },
      { path: "/goods", component: Goods },
      { path: "/goods/add", component: GoodsAdd },
      { path: "/params", component: Params },
      { path: "/categories", component: Categories },
      { path: "/orders", component: Order },
      { path: "/reports", component: Reports }
    ]
  }
];

const router = new VueRouter({
  routes
});

//路由拦截
/* to====要去的路径
from==当前的路径
next====函数  放行 */

router.beforeEach((to, from, next) => {
  // console.log(to);
  // console.log(from);
  if (to.path == "/Login") {
    return next();
  }

  var token = window.sessionStorage.getItem("token");
  //没有token
  if (!token) {
    return next("/Login");
  }
  return next();
});
// 解决路由版本的兼容性的;
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

export default router;
